package com.txx.app.main.modules.web.function;

import android.content.Intent;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.qihoo360.replugin.RePlugin;
import com.txx.app.main.module.knowledge.bean.TkMainTaikListBean;
import com.txx.app.main.module.msg.webview.JumpToMeAgentUtil;
import com.txx.app.main.module.msg.webview.JumpToMsgUtil;
import com.txx.app.main.modules.web.WebCommonFragment;
import com.txx.app.main.modules.web.js.H5BasePlugin;
import com.txx.app.main.modules.web.js.H5Event;

import org.json.JSONObject;

import static com.qihoo360.replugin.RePlugin.createIntent;


/**
 * 加密的sdk
 */
public class JumpToNativePage extends H5BasePlugin {
    private String TAG = JumpToNativePage.class.getSimpleName ();
    public String refreshCallBackName;
    private WebCommonFragment fg;

    public JumpToNativePage() {
    }

    @Override
    public boolean handleEvent(H5Event h5Event, final WebCommonFragment fragment) {

        if (h5Event == null) {
            return false;
        }
        try {
            String json = h5Event.getJsonSource ();
            // 解析H5页面的入参
            JSONObject jsonObject = new JSONObject (json);
            switch (jsonObject.getString ("page")) {

                case "29"://待办—提醒
                    String remindType = jsonObject.getString ("remindType");
                    refreshCallBackName = jsonObject.getString ("callBackName");
                    fg = fragment;
                    JumpToMsgUtil.jumpToRemind (fragment.getContext (), remindType);

                    break;
                case "32":
                    Intent intent = createIntent ("app.webview", "com.txx.app.webview.ui.H5WebActivity");
                    intent.putExtra ("url", jsonObject.getString ("url"));
                    refreshCallBackName = jsonObject.getString ("callBackName");
                    fg = fragment;
                    RePlugin.startActivity (fragment.getContext (), intent);
                    break;
                case "33":
                    String remindTypeMeAgent = jsonObject.getString ("remindType");
                    refreshCallBackName = jsonObject.getString ("callBackName");
                    fg = fragment;
                    JumpToMeAgentUtil.jumpToRemind (fragment.getContext (), remindTypeMeAgent);
                    break;
                case "34":
                    Intent confirmation = createIntent ("app.policy", "com.txx.app.policy.commom.confirmation_letter.ConfirmationLetterApplicationActivity");
                    RePlugin.startActivity (fragment.getContext (), confirmation);
                    break;
                case "38":
                    Intent webview = createIntent ("app.webview", "com.txx.app.webview.ui.H5WebActivity");
                    webview.putExtra ("url", jsonObject.optString ("url"));
                    webview.putExtra ("shareInfo", jsonObject.optString ("shareInfo"));
                    webview.putExtra ("shareQyInfo", jsonObject.optString ("shareQyInfo"));
                    webview.putExtra ("taiLifeInfo", jsonObject.optString ("taiLifeInfo"));
                    webview.putExtra ("qrCodeInfo", jsonObject.optString ("qrCodeInfo"));
                    refreshCallBackName = jsonObject.getString ("callBackName");
                    fg = fragment;
                    RePlugin.startActivity (fragment.getContext (), webview);
                    break;
                case "49":// 首页贺卡跳转老版本webview详情
                {
                    String info = jsonObject.optString ("shareInfo");
                    if (!TextUtils.isEmpty (info)) {
                        TkMainTaikListBean tkMainTtListBean = new Gson ().fromJson (info, TkMainTaikListBean.class);
                        Intent main_webview = createIntent ("app.webview", "com.txx.app.webview.oldwebview.WebViewActivity");
                        Boolean ifTrans;//能否转发
                        if ("0".equals (tkMainTtListBean.getIfTrans ())) {
                            ifTrans = true;
                        } else {
                            ifTrans = false;
                        }
                        main_webview.putExtra ("url", tkMainTtListBean.getTurnUrl ());
                        main_webview.putExtra ("title", tkMainTtListBean.getTitle ());
                        main_webview.putExtra ("canShare", ifTrans);
                        main_webview.putExtra ("addParam", false);
                        main_webview.putExtra ("mssFlag", tkMainTtListBean.getMssFlag ());
                        main_webview.putExtra ("logoUrl", tkMainTtListBean.getIconImgUrl ());
                        main_webview.putExtra ("description", tkMainTtListBean.getTransDesc ());
                        main_webview.putExtra ("id", tkMainTtListBean.getId ());
                        main_webview.putExtra ("imgUrl", tkMainTtListBean.getFaceImgUrl ());
                        main_webview.putExtra ("type", 700);
                        RePlugin.startActivity (fragment.getContext (), main_webview);
                    }

                }
                break;
                case "58":
                {
                    Intent intent_search = RePlugin.createIntent("app.jiazu", "com.txx.app.jiazu.jiazu_module.main.activity.FamilyMainActivity");
                    RePlugin.startActivity(fragment.getContext(), intent_search);
                }
                break;
            }


        } catch (Exception e) {
            e.printStackTrace ();
        }
        return true;
    }

    @Override
    public void onReusme() {

        if (!TextUtils.isEmpty (refreshCallBackName) && fg != null) {
            // 处理后调用
            fg.getH5CallBackCommon (refreshCallBackName, "");
        }

    }

    @Override
    public void onRelease() {
        if (fg != null) {
            fg = null;
        }
    }


}
